/* eslint-disable vue/valid-v-for */
<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div class='logo'>
        <img
          src="../../assets/img/heima.png"
          alt=""
        >
        <h2>电商管理系统</h2>
      </div>
      <el-button
        type="info"
        @click='back'
      >退出</el-button>
    </el-header>
    <!-- 主题区域  -->
    <el-container class='content'>
      <!-- 侧边栏 -->
      <el-aside
        :width="isCollapse ? '64px':'200px'"
        class='side'
      >
        <div
          class="toggle"
          @click='toggle'
        >|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened='true'
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id+''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title"><i :class='iconlist[item.id]'></i><span>{{item.authName}}</span></template>
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click='setActivePath("/"+subItem.path)'
            >
              <template slot="title"><i class="el-icon-box"></i><span>{{subItem.authName}}</span></template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域  -->
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部区域 -->
        <el-footer>
          <span>Just for test</span>
          <span>by harry</span>
        </el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    // console.log(this.activePath)
  },
  data () {
    return {
      menulist: [],
      iconlist: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    back () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    setActivePath (index) {
      window.sessionStorage.setItem('activePath', index)
      this.activePath = index
    }
  }
}
</script>

<style scoped>
@import url(../../assets/css/home.css);
</style>
